package cn.maihe.elg.operation.supports.ocr;

import cn.maihe.elg.operation.config.AliyunOcrConfig;
import cn.maihe.elg.operation.utils.AliyunOcrHelper;
import com.alibaba.cloudapi.sdk.constant.SdkConstant;
import com.alibaba.cloudapi.sdk.model.ApiResponse;
import com.alibaba.cloudapi.sdk.model.HttpClientBuilderParams;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @Description
 * @Author caixh
 * @Date 2021/8/5.
 */
@Service
@Slf4j
public class AliyunOcrService {
    private final AliyunOcrConfig aliyunOcrConfig;
    private final AliyunOcrHelper idcardOcr;
    private final AliyunOcrHelper businessLicense;
    private final AliyunOcrHelper openLicense;

    public AliyunOcrService(AliyunOcrConfig aliyunOcrConfig) {
        this.aliyunOcrConfig = aliyunOcrConfig;
        HttpClientBuilderParams idcardHttpsParam = new HttpClientBuilderParams();
        idcardHttpsParam.setAppKey(aliyunOcrConfig.getAppKey());
        idcardHttpsParam.setAppSecret(aliyunOcrConfig.getAppSecret());
        idcardHttpsParam.setHost(aliyunOcrConfig.getIdCard().getHost());
        idcardOcr = new AliyunOcrHelper();
        idcardOcr.init(idcardHttpsParam);

        HttpClientBuilderParams businessLicenseParams = new HttpClientBuilderParams();
        businessLicenseParams.setAppKey(aliyunOcrConfig.getAppKey());
        businessLicenseParams.setAppSecret(aliyunOcrConfig.getAppSecret());
        businessLicenseParams.setHost(aliyunOcrConfig.getBusinessLicense().getHost());
        businessLicense = new AliyunOcrHelper();
        businessLicense.init(businessLicenseParams);

        HttpClientBuilderParams openLicenseHttpsParam = new HttpClientBuilderParams();
        openLicenseHttpsParam.setAppKey(aliyunOcrConfig.getAppKey());
        openLicenseHttpsParam.setAppSecret(aliyunOcrConfig.getAppSecret());
        openLicenseHttpsParam.setHost(aliyunOcrConfig.getOpenLicense().getHost());
        openLicense = new AliyunOcrHelper();
        openLicense.init(openLicenseHttpsParam);
    }

    public ApiResponse ocr(String type, String imageBase64) {
        if (type.equals("idcard0")) {
            //configure配置
            JSONObject configObj = new JSONObject();
            configObj.put("side", "face");

            String config_str = configObj.toString();

            // 拼装请求body的json字符串
            JSONObject requestObj = new JSONObject();
            requestObj.put("image", imageBase64);
            if (configObj.size() > 0) {
                requestObj.put("configure", config_str);
            }
            String bodys = requestObj.toString();
            return idcardOcr.ocrSyncMode(bodys.getBytes(SdkConstant.CLOUDAPI_ENCODING), aliyunOcrConfig.getIdCard().getRestUrl());
        } else if (type.equals("idcard1")) {
            //configure配置
            JSONObject configObj = new JSONObject();
            configObj.put("side", "back");

            String config_str = configObj.toString();

            // 拼装请求body的json字符串
            JSONObject requestObj = new JSONObject();
            requestObj.put("image", imageBase64);
            if (configObj.size() > 0) {
                requestObj.put("configure", config_str);
            }
            String bodys = requestObj.toString();
            return idcardOcr.ocrSyncMode(bodys.getBytes(SdkConstant.CLOUDAPI_ENCODING), aliyunOcrConfig.getIdCard().getRestUrl());
        } else if (type.equals("business")) {
            String bodys = "{\"image\":\"" + imageBase64 + "\"}";
            return businessLicense.ocrSyncMode(bodys.getBytes(SdkConstant.CLOUDAPI_ENCODING), aliyunOcrConfig.getBusinessLicense().getRestUrl());
        } else if (type.equals("open")) {
            return openLicense.ocrSyncMode("0", imageBase64, aliyunOcrConfig.getOpenLicense().getRestUrl());
        }
        return null;
    }

}
